| Total Complexity | 3 |
| Total Lines | 21 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import {CommandHandler} from '@nestjs/cqrs'; |
||
| 8 | |||
| 9 | @CommandHandler(DeleteActivityCommand) |
||
| 10 | export class DeleteActivityCommandHandler { |
||
| 11 | constructor( |
||
| 12 | @Inject('IActivityRepository') |
||
| 13 | private readonly activityRepository: IActivityRepository |
||
| 14 | ) {} |
||
| 15 | |||
| 16 | public async execute(command: DeleteActivityCommand): Promise<void> { |
||
| 17 | const {activityId, user} = command; |
||
| 18 | const activity = await this.activityRepository.findOneById(activityId); |
||
| 19 | |||
| 20 | if (!(activity instanceof Activity)) { |
||
| 21 | throw new ActivityNotFoundException(); |
||
| 22 | } |
||
| 23 | |||
| 24 | if (activity.getUser().getId() !== user.getId()) { |
||
| 25 | throw new NotActivityOwnerException(); |
||
| 26 | } |
||
| 27 | |||
| 28 | this.activityRepository.deleteById(activityId); |
||
| 29 | } |
||
| 31 |